package pactice_handler

import (
	"fmt"
	"peilian-api/app/global/bind"
	"peilian-api/app/global/common"
	"peilian-api/app/global/databases/tables"
	"peilian-api/app/global/errors"
	"peilian-api/app/global/variable"
	"peilian-api/app/model"
	"peilian-api/app/services/resume_service"
	"peilian-api/utils/format"
	"strconv"

	"github.com/gin-gonic/gin"
)

func GetOptResumeList(c *gin.Context) {
	var gorl model.GetOptResumeListReq
	if err := bind.ShouldBinding(c, &gorl); err != nil {
		format.NewResponseJson(c).Err(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	count, out, err := new(resume_service.ResumeService).GetOptResumeList(c, gorl)
	if err != nil {
		format.NewResponseJson(c).Err(err.Code, err.Err.Error())
		return
	}
	format.NewResponseJson(c).SuccessDataList(int(count), out)
}

func GetOptResumeItem(c *gin.Context) {
	id := c.Param("id")
	if len(id) == 0 {
		format.NewResponseJson(c).Err(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	rid, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		format.NewResponseJson(c).Err(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	gori := model.GetOptResumeItemReq{Id: uint(rid)}
	opt_resume, err_ := new(resume_service.ResumeService).GetOptResumeItem(c, gori)
	if err_ != nil {
		format.NewResponseJson(c).Err(err_.Code, err_.Err.Error())
		return
	}
	format.NewResponseJson(c).SuccessObj(opt_resume)
}

func AddOptResumeList(c *gin.Context) {
	var gor model.GenerateOptReq
	if err := bind.ShouldBinding(c, &gor); err != nil {
		format.NewResponseJson(c).ErrObj(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	rs := new(resume_service.ResumeService)
	err := rs.GenerateOpt(c, gor)
	if err != nil {
		format.NewResponseJson(c).Err(err.Code, err.Err.Error())
		return
	}

	// var resume_record tables.ResumeRecord
	// if err := c.ShouldBindJSON(&resume_record); err != nil {
	// 	format.NewResponseJson(c).Err(30001, err.Error())
	// 	return
	// }
	// uid, exists := c.Get("uid")
	// if !exists {
	// 	format.NewResponseJson(c).Err(30001, "您无权操作")
	// 	return
	// }
	// if tools.HanZiLen(resume_record.Exp) > 500 {
	// 	format.NewResponseJson(c).Err(30001, "题目内容不能超过500个字")
	// 	return
	// }
	// resume_record.User_id = uid.(uint)
	// if err := variable.DB.MySQL.Transaction(func(tx *gorm.DB) error {
	// 	resume_record.ResumeStatus = 0 // 设置为未生成
	// 	resume_record.Opt_exp = ""
	// 	if err := variable.DB.MySQL.Create(&resume_record).Error; err != nil {
	// 		return err
	// 	}
	// 	return nil
	// }); err != nil {
	// 	format.NewResponseJson(c).Err(http.StatusInternalServerError, err.Error())
	// 	return
	// }
	// sc := variable.DB.Redis.Set(fmt.Sprintf(common.RedisResumeIsViewedKey, resume_record.ID), resume_record.IsViewed, 0)
	// _, err := sc.Result()
	// if err != nil {
	// 	return //失败也要返回
	// }
	format.NewResponseJson(c).SuccessObj(struct{}{})

}

func UpdateOptResumeList(c *gin.Context) {
	var uori model.UpdateOptResumeItemReq
	if err := bind.ShouldBinding(c, &uori); err != nil {
		format.NewResponseJson(c).ErrObj(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	err_ := new(resume_service.ResumeService).UpdateOptResumeItem(c, uori)
	if err_ != nil {
		format.NewResponseJson(c).Err(err_.Code, err_.Err.Error())
		return
	}
	format.NewResponseJson(c).SuccessObj(struct{}{})
}

func DeleteOptResume(ctx *gin.Context) {
	resume_id := ctx.Param("id")
	uid, exists := ctx.Get("uid")
	if !exists {
		format.NewResponseJson(ctx).Err(30001, "您无权操作")
		return
	}
	resume := tables.ResumeRecord{}
	d := variable.DB.MySQL.Where("id = ?", resume_id).Find(&resume)
	if d.RowsAffected == 0 {
		format.NewResponseJson(ctx).Err(30001, "优化简历记录不存在")
		return
	}
	if resume.User_id != uid.(uint) {
		format.NewResponseJson(ctx).Err(30001, "不能删除非您创建的简历优化记录！")
		return
	}
	if resume.ResumeStatus != 2 {
		format.NewResponseJson(ctx).Err(30001, "该条优化记录在生成中，不能被删除")
		return
	}
	variable.DB.MySQL.Delete(&tables.ResumeRecord{}, "id=?", resume_id)

	variable.DB.Redis.ZRem(fmt.Sprintf(common.RedisResumeIsViewedKey, resume.User_id), resume.ID)

	format.NewResponseJson(ctx).SuccessObj(struct{}{})
}

// 重新生成优化简历
func ReGenerateOpt(c *gin.Context) {
	var gor model.GenerateOptReq
	if err := bind.ShouldBinding(c, &gor); err != nil {
		format.NewResponseJson(c).ErrObj(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	err := new(resume_service.ResumeService).GenerateOpt(c, gor)
	if err != nil {
		format.NewResponseJson(c).ErrObj(err.Code, err.Err.Error())
		return
	}
	format.NewResponseJson(c).SuccessObj(struct{}{})
}

func GetOptResumeState(c *gin.Context) {
	id := c.Param("id")
	if len(id) == 0 {
		format.NewResponseJson(c).Err(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	rid, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		format.NewResponseJson(c).Err(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	status, err_ := new(resume_service.ResumeService).GetOptResumeIsViewedById(c, int(rid))
	if err_ != nil {
		format.NewResponseJson(c).Err(err_.Code, err_.Err.Error())
		return
	}
	format.NewResponseJson(c).SuccessObj(status)
}

func CheckIsAllViewed(c *gin.Context) {
	uid, exists := c.Get("uid")
	if !exists {
		format.NewResponseJson(c).Err(30001, "您无权操作")
		return
	}
	is_all_viewed, err := new(resume_service.ResumeService).CheckIsAllViewed(c, uid.(uint))
	if err != nil {
		format.NewResponseJson(c).Err(err.Code, err.Err.Error())
		return
	}
	format.NewResponseJson(c).SuccessObj(is_all_viewed)
}

func CheckIsAllOpt(c *gin.Context) {
	uid, exists := c.Get("uid")
	if !exists {
		format.NewResponseJson(c).Err(30001, "您无权操作")
		return
	}
	var num = 0
	// 是否中有生成中的简历的数量
	ssc := variable.DB.Redis.ZRange(fmt.Sprintf(common.RedisResumeIsGenerateKey, uid), 0, -1)
	if ssc.Err() != nil {
		num = 0
	} else {
		sids, _ := ssc.Result()
		num = len(sids)
	}

	// 是否全部已查看
	isView, err := new(resume_service.ResumeService).CheckIsAllViewed(c, uid.(uint))
	if err != nil {
		format.NewResponseJson(c).Err(err.Code, err.Err.Error())
		return
	}

	format.NewResponseJson(c).SuccessObj(struct {
		Create int `json:"create"`
		IsView int `json:"is_view"`
	}{Create: num, IsView: isView})
}
